#include "Life.h"
#include "Map.h"

Life::Life()
	:mActive(true)
	,mLife(0)
	
	
{
};

Life::~Life()
{

};

void Life::Load()
{
	
	mSprite.Load("Heart.png");
	
}; // initialize

void Life::Unload()
{
	mSprite.Unload();
}; // terminate

void Life::Update(float deltaTime, const Map& map)
{
	if(mActive)
	{
	const float Kspeed = 500.0f;

	if(Input_IsKeyDown(Keys::D))
	{
		mPosition.x+=Kspeed * deltaTime;
	}
	else if(Input_IsKeyDown(Keys::A))
	{
		mPosition.x-=Kspeed * deltaTime;
	}
	if(Input_IsKeyDown(Keys::W))
	{
		mPosition.y-=Kspeed * deltaTime;
	}
	else if(Input_IsKeyDown(Keys::S))
	{
		mPosition.y+=Kspeed * deltaTime;
	}



	if(mPosition.x <= 0.0f - (mSprite.GetWidth() * 0.5f))
	{
		
		
			mPosition.x = map.GetWidth() - 0.1f;
		
	}

	else if(mPosition.x >= map.GetWidth() + (mSprite.GetWidth() * 0.5f))
	{
		mPosition.x = mSprite.GetWidth()/2;
	}

	
	if(mPosition.y <= 0.0f - (mSprite.GetHeight() * 0.5f))
	{
		mPosition.y = map.GetHeight() - 0.5f;
	}
	else if(mPosition.y >= map.GetHeight() + (mSprite.GetHeight() * 0.5f))
	{
		mPosition.y = 0.0f;
	}


	}


	//------------------------------------------------------------------------------------------------------>




	// speed of the Life
	//const float kSpeed = 400.0f;
	//float displacementX = 0.0f;
	//float displacementY = 0.0f;
	//// x values ----------------------------------------
	//if (Input_IsKeyDown(Keys::RIGHT))
	//{
	//	displacementX = kSpeed * deltaTime;
	//}
	//else if (Input_IsKeyDown(Keys::LEFT))
	//{
	//	displacementX = -kSpeed * deltaTime;
	//}

	//else if (Input_IsKeyDown(Keys::UP))
	//{
	//	displacementY = -kSpeed * deltaTime;
	//}
	//else if (Input_IsKeyDown(Keys::DOWN))
	//{
	//	displacementY = kSpeed * deltaTime;
	//}


	//SRect boundingBox = GetBoundingBox() + SVector2(displacementX, 0.0f);
	//if (displacementX > 0.0f)
	//{
	//	// check right
	//	SLineSegment segment = boundingBox.GetRightSegment();
	//	SRect collisionBox;
	//	if (map.TestCollision(segment, collisionBox))
	//	{
	//		displacementX -= (boundingBox.max.x - collisionBox.min.x+ 1.0f);
	//		mPosition.x += displacementX;
	//	}
	//	else 
	//	{
	//		mPosition.x += displacementX;
	//	}
	//}
	//else if (displacementX < 0.0f)
	//{
	//	// TODO: check left
	//	SLineSegment segment = boundingBox.GetLeftSegment();
	//	SRect collisionBox;
	//	if (map.TestCollision(segment, collisionBox))
	//	{
	//		displacementX += (collisionBox.max.x - boundingBox.min.x+1.0f);
	//		mPosition.x += (int)displacementX;
	//	}
	//	else
	//	{
	//		mPosition.x+=(int)displacementX;
	//	}
	//}

	//// y values -----------------------------------------
	//


	//boundingBox=GetBoundingBox() + SVector2(0.0f,displacementY);
	//if(displacementY > 0.0f)
	//{
	//	//checkdown
	//	SLineSegment segment = boundingBox.GetBottomSegment();
	//	SRect collisionBox;
	//	if (map.TestCollision(segment, collisionBox))
	//	{
	//		displacementY -= (boundingBox.max.y - collisionBox.min.y+1.0f);
	//		mPosition.y += (int)displacementY;
	//	}
	//	else
	//	{
	//		mPosition.y+=(int)displacementY;
	//	}
	//}


	//else if(displacementY < 0.0f)
	//{
	//	//check up
	//	SLineSegment segment = boundingBox.GetTopSegment();
	//	SRect collisionBox;
	//	if (map.TestCollision(segment, collisionBox))
	//	{
	//		displacementY += (collisionBox.max.y - boundingBox.min.y+1.0f);
	//		mPosition.y += (int)displacementY;
	//	}
	//	else
	//	{
	//		mPosition.y+=(int)displacementY;
	//	}
	//}

};

void Life::Render(const SVector2& viewOffset)
{
	if(mActive)
	{
	int width = mSprite.GetWidth();
	int height = mSprite.GetHeight();
	SVector2 offset(width * 0.5f, height * 0.5f);
	SVector2 renderPosition = mPosition - offset - viewOffset; 
	mSprite.SetPosition(renderPosition);
	mSprite.Render();
	}
};




void Life::Kill()
{
	
	mActive=false;
}


bool Life::IsDead()
{
	/*if (mLife==0)
	{*/
		Kill();
		return true;
	/*}*/
	/*else
	{
	--mLife;
	return false;
	}*/
}



//SRect Life::GetBoundingBox() const
//{
//	SRect boundingBox;
//	boundingBox.min.x = mPosition.x - 32.0f;
//	boundingBox.max.x = mPosition.x + 32.0f;
//
//	boundingBox.min.y = mPosition.y - 50.0f;
//	boundingBox.max.y = mPosition.y + 50.0f;
//	return boundingBox;
//}